package com.chuantou.yuba.ui.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.chuantou.yuba.R;
import com.chuantou.yuba.api.to.BDPoiTO;
import com.chuantou.yuba.api.to.CityTO;
import com.chuantou.yuba.api.to.FishingShopTO;
import com.chuantou.yuba.ui.InputAddressActivity;
import com.chuantou.yuba.util.Constants;

/**
 * 发布品牌商界面
 * 
 * @author dl1001
 * 
 */
public class PublishPinPaiShangFragment extends AbsPublishNearFishingFragment {

	private View mContentView;
	private EditText mNameView;
	private TextView mDistrictView;
	private TextView mAddressView;
	private EditText mIntroduceView;
	private TextView mIntroduceCountView;
	private RecyclerView mRecyclerView;
	private EditText mContactView;
	private EditText mPhoneView;
	private EditText mShopUrlView;

	private CityTO mProvince;
	private CityTO mCity;
	private FishingShopTO mShop;

	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		mShop = getArguments().getParcelable(Constants.EXTRA_SHOP);
		if (mContentView == null) {
			mContentView = inflater.inflate(R.layout.publish_pinpaishang, null);
			mContentView.findViewById(R.id.district_layout).setOnClickListener(this);
			mNameView = (EditText) mContentView.findViewById(R.id.brand);
			mDistrictView = (TextView) mContentView.findViewById(R.id.district);
			mAddressView = (TextView) mContentView.findViewById(R.id.address);
			mContactView = (EditText) mContentView.findViewById(R.id.contact);
			mPhoneView = (EditText) mContentView.findViewById(R.id.phone);
			mIntroduceView = (EditText) mContentView.findViewById(R.id.introduce);
			mIntroduceCountView = (TextView) mContentView.findViewById(R.id.introduce_count);
			mIntroduceView.addTextChangedListener(new TextWatcher() {

				@Override
				public void onTextChanged(CharSequence s, int start, int before, int count) {

				}

				@Override
				public void beforeTextChanged(CharSequence s, int start, int count, int after) {

				}

				@Override
				public void afterTextChanged(Editable s) {
					int length = mIntroduceView.getText().length();
					mIntroduceCountView.setText(String.valueOf(200 - length));
				}
			});
			mShopUrlView = (EditText) mContentView.findViewById(R.id.shop_url);
			mRecyclerView = (RecyclerView) mContentView.findViewById(R.id.recyclerView);
			LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
			layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
			mRecyclerView.setLayoutManager(layoutManager);
			mRecyclerView.setAdapter(mImageAdapter);
			mContentView.findViewById(R.id.address_layout).setOnClickListener(this);
			if (mShop != null) {
				mNameView.setText(mShop.getName());
				String district = String.format("%s %s", mShop.getStateName(), mShop.getCityName());
				mProvince = new CityTO();
				mProvince.setName(mShop.getStateName());
				mProvince.setId(mShop.getStateId());
				mCity = new CityTO();
				mCity.setName(mShop.getCityName());
				mCity.setId(mShop.getCityId());
				mDistrictView.setText(district);
				mAddressView.setText(mShop.getAddress());
				mContactView.setText(mShop.getContact());
				mPhoneView.setText(mShop.getPhone());
				mIntroduceView.setText(mShop.getContent());
				setCoverView(mShop.getImages());
				mShopUrlView.setText(mShop.getShopUrl());
			}
		}
		ViewGroup parentView = (ViewGroup) mContentView.getParent();
		if (parentView != null) {
			parentView.removeView(mContentView);
		}
		return mContentView;
	}

	@Override
	public void onSubmit() {

		if (mProvince == null || mCity == null) {
			Toast.makeText(mContext, R.string.input_district_error, Toast.LENGTH_SHORT).show();
			return;
		}
		String address = mAddressView.getText().toString().trim();
		if (TextUtils.isEmpty(address)) {
			Toast.makeText(mContext, R.string.input_fishing_detail_address_error, Toast.LENGTH_SHORT).show();
			return;
		}
		String name = mNameView.getText().toString().trim();
		if (TextUtils.isEmpty(name)) {
			Toast.makeText(mContext, R.string.input_name_error, Toast.LENGTH_SHORT).show();
			return;
		}
		String contact = mContactView.getText().toString().trim();
		if (TextUtils.isEmpty(contact)) {
			Toast.makeText(mContext, R.string.input_contact_error, Toast.LENGTH_SHORT).show();
			return;
		}
		String phone = mPhoneView.getText().toString().trim();
		if (TextUtils.isEmpty(phone)) {
			Toast.makeText(mContext, R.string.input_telephone_error, Toast.LENGTH_SHORT).show();
			return;
		}
		String introduce = mIntroduceView.getText().toString().trim();
		if (TextUtils.isEmpty(introduce)) {
			Toast.makeText(mContext, R.string.input_introduce_error, Toast.LENGTH_SHORT).show();
			return;
		}
		String shopUrl = mShopUrlView.getText().toString().trim();
		long shopId = mShop == null ? -1 : mShop.getId();
		uploadFishingShop(shopId, 2, mProvince.getId(), mCity.getId(), name, introduce, contact, phone, address, shopUrl);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.district_layout:
			showProvinceDialog();
			break;
		case R.id.address_layout:
			startActivityForResult(new Intent(getActivity(), InputAddressActivity.class), REQUEST_CODE_ADDRESS);
			break;
		default:
			super.onClick(v);
			break;
		}
	}

	@Override
	public void onCityPicked(CityTO province, CityTO city) {
		mProvince = province;
		mCity = city;
		String district = String.format("%s %s", mProvince.getName(), mCity.getName());
		mDistrictView.setText(district);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode != Activity.RESULT_OK)
			return;
		switch (requestCode) {
		case REQUEST_CODE_ADDRESS:
			BDPoiTO poi = data.getParcelableExtra(Constants.EXTRA_DATA);
			mAddressView.setText(poi.getAddress());
			break;
		default:
			break;
		}
	}
}
